package featureExtraction;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;

import javax.imageio.ImageIO;


//import omrPreprocess.ImageLoader;

public class TokenRaw
{	
	public Integer X0, Y0, X1, Y1; //Inclusive rectangular token.
//	public byte[][] SegmentArray;
	public Integer AvgX, AvgY;
	
	
	public TokenRaw()
	{
	}
	
	public TokenRaw(int x0, int x1, int y0, int y1)
	{
		this.X0 = x0;
		this.X1 = x1;
		this.Y0 = y0;
		this.Y1 = y1;
		this.AvgX = (x0 + x1) / 2;
		this.AvgY = (y0 + y1) / 2;
	}
	
	public void CreateToken(BufferedImage image, byte[][] workingImage, Integer x, Integer y)
	{
		workingImage[x][y] = 2;
		this.X0 = x;
		this.X1 = x;
		this.Y0 = y;
		this.Y1 = y;
		boolean[] sideIncremented = new boolean[4];
		boolean anyIncrements = true;
		
		while (anyIncrements == true)
		{

			anyIncrements = false;
			for (int i = 0; i < 4; i++)
				sideIncremented[i] = false;
			{
				for (int i = X0 - 1; i <= X1 + 1; i++)
				{
					if ((workingImage[i][Y0] == 2 || 
							(workingImage[i - 1][Y0] == 2) || 
							(workingImage[i + 1][Y0] == 2)) && 
							image.getRGB(i, Y0 - 1) != 0xFFFFFFFF)
					{
						workingImage[i][Y0 - 1] = 3;
					}
				}
				for (int i = X0 - 1; i <= X1 + 1; i++)
				{
					if (workingImage[i][Y0 - 1] == 3)
					{
						sideIncremented[0] = true;
						for (int j = i; j >= X0 - 1; j--)
						{
							if (image.getRGB(j, Y0 - 1) != 0xFFFFFFFF || workingImage[j][Y0 - 1] == 3)
								workingImage[j][Y0 - 1] = 2;
							else
								break;
						}
						for (int j = i; j <=  X1 + 1; j++)
						{
							if (image.getRGB(j, Y0 - 1) != 0xFFFFFFFF || workingImage[j][Y0 - 1] == 3)
								workingImage[j][Y0 - 1] = 2;
							else
								break;
						}
					}
				}
			}
			{
				for (int i = X0 - 1; i <= X1 + 1; i++)
				{
					if ((workingImage[i][Y1] == 2 || 
							(workingImage[i - 1][Y1] == 2) || 
							(workingImage[i + 1][Y1] == 2)) && 
							image.getRGB(i, Y1 + 1) != 0xFFFFFFFF)
					{
						workingImage[i][Y1 + 1] = 3;
					}
				}
				for (int i = X0 - 1; i <= X1 + 1; i++)
				{
					if (workingImage[i][Y1 + 1] == 3)
					{
						sideIncremented[1] = true;
						for (int j = i; j >= X0 - 1; j--)
						{
							if (image.getRGB(j, Y1 + 1) != 0xFFFFFFFF || workingImage[j][Y1 + 1] == 3)
								workingImage[j][Y1 + 1] = 2;
							else
								break;
						}
						for (int j = i; j <=  X1 + 1; j++)
						{
							if (image.getRGB(j, Y1 + 1) != 0xFFFFFFFF || workingImage[j][Y1 + 1] == 3)
								workingImage[j][Y1 + 1] = 2;
							else
								break;
						}
					}
				}
			}
			{
				for (int i = Y0 - 1; i <= Y1 + 1; i++)
				{
					if ((workingImage[X0][i] == 2 || 
							(workingImage[X0][i - 1] == 2) || 
							(workingImage[X0][i + 1] == 2)) && 
							image.getRGB(X0 - 1, i) != 0xFFFFFFFF)
					{
						workingImage[X0 - 1][i] = 3;
					}
				}
				for (int i = Y0 - 1; i <= Y1 + 1; i++)
				{
					if (workingImage[X0 - 1][i] == 3)
					{
						sideIncremented[2] = true;
						for (int j = i; j >= Y0 - 1; j--)
						{
							if (image.getRGB(X0 - 1, j) != 0xFFFFFFFF || workingImage[X0 - 1][j] == 3)
								workingImage[X0 - 1][j] = 2;
							else
								break;
						}
						for (int j = i; j <=  Y1 + 1; j++)
						{
							if (image.getRGB(X0 - 1, j) != 0xFFFFFFFF || workingImage[X0 - 1][j] == 3)
								workingImage[X0 - 1][j] = 2;
							else
								break;
						}
					}
				}
			}
			{
				for (int i = Y0 - 1; i <= Y1 + 1; i++)
				{
					if ((workingImage[X1][i] == 2 || 
							(workingImage[X1][i - 1] == 2) || 
							(workingImage[X1][i + 1] == 2)) && 
							image.getRGB(X1 + 1, i) != 0xFFFFFFFF)
					{
						workingImage[X1 + 1][i] = 3;
					}
				}
				for (int i = Y0 - 1; i <= Y1 + 1; i++)
				{
					if (workingImage[X1 + 1][i] == 3)
					{
						sideIncremented[3] = true;
						for (int j = i; j >= Y0 - 1; j--)
						{
							if (image.getRGB(X1 + 1, j) != 0xFFFFFFFF || workingImage[X1 + 1][j] == 3)
								workingImage[X1 + 1][j] = 2;
							else
								break;
						}
						for (int j = i; j <=  Y1 + 1; j++)
						{
							if (image.getRGB(X1 + 1, j) != 0xFFFFFFFF || workingImage[X1 + 1][j] == 3)
								workingImage[X1 + 1][j] = 2;
							else
								break;
						}
					}
				}
			}
	
			if (sideIncremented[0] == true && Y0 - 1 == 1)
				sideIncremented[0] = false;
			if (sideIncremented[1] == true && Y1 + 1 == image.getHeight() - 2)
				sideIncremented[1] = false;
			if (sideIncremented[2] == true && X0 - 1 == 1)
				sideIncremented[2] = false;
			if (sideIncremented[3] == true && X1 + 1 == image.getWidth() - 2)
				sideIncremented[3] = false;
			
			for (int i = 0; i < 4; i++)
			{
				if (sideIncremented[i] == true)
					anyIncrements = true;
			}
			
			if (sideIncremented[0] == true)
				Y0 = Y0 - 1;
			if (sideIncremented[1] == true)
				Y1 = Y1 + 1;
			if (sideIncremented[2] == true)
				X0 = X0 - 1;
			if (sideIncremented[3] == true)
				X1 = X1 + 1;
		}
		
		
//		{
//			System.out.println("here");
//			for (int i = 0; i < workingImage.length; i++)
//			{
//				for (int j = 0; j < workingImage[i].length; j++)
//				{
//					if (workingImage[i][j] != 0)
//						workingImage[i][j] = (byte)255;
//				}
//			}
//			String userFolder = System.getProperty("user.dir");
//			File outputFile = new File(userFolder + "\\file_out_1.png");
//			//ImageIO.write(originalImage, "png", outputFile);
//			try {
//				ImageIO.write(ImageArrayUtil.ArrayToImage(workingImage), "png", outputFile);
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		
		
		for (int i = X0; i <= X1; i++)
		{
			for (int j = Y0; j <= Y1; j++)
			{
				if (workingImage[i][j] == 2)
					workingImage[i][j] = 1;
			}
		}
		
		 this.AvgX = (X0 + X1) / 2;
		 this.AvgY = (Y0 + Y1) / 2;
	}
	

	
	
}
